<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="org.example.entity.Person" %>
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>投票系统 - 管理员主页</title>
    <!-- 引入Layui CSS -->
    <%--    <link rel="stylesheet" href="../../css/layui.min.css">--%>
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.8.17/css/layui.min.css">
    <script src="../../js/layui.js"></script>
    <script src="../../js/jquery-3.7.1.min.js"></script>
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            color: #333;
        }

        .action-buttons {
            display: flex;
            gap: 8px;
        }

        .action-buttons button {
            background-color: transparent;
            border: none;
            cursor: pointer;
            padding: 5px;
            border-radius: 5px;
            transition: all 0.3s ease;
        }

        .action-buttons .edit-btn {
            color: #1E9FFF;
        }

        .action-buttons .edit-btn:hover {
            background-color: rgba(30, 159, 255, 0.1);
        }

        .action-buttons .delete-btn {
            color: #dc3545;
        }

        .action-buttons .delete-btn:hover {
            background-color: rgba(220, 53, 69, 0.1);
        }

        .header {
            background-color: #ffffff;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            padding: 15px 30px;
            display: flex;
            align-items: center;
        }

        .header a {
            color: #333;
            margin-left: 25px;
            text-decoration: none;
            transition: color 0.3s ease;
            font-size: 15px;
        }

        .header a:hover {
            color: #1E9FFF;
        }

        .header > a:first-child {
            margin-left: 0;
        }

        .ml-auto {
            margin-left: auto;
        }

        .admin-container {
            padding: 30px 20px;
            max-width: 1400px;
            margin: 0 auto;
        }

        .layui-tab {
            margin: 0;
            background-color: #ffffff;
            border-radius: 16px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
            overflow: hidden;
        }

        .layui-tab-title {
            border-bottom: none;
            padding: 0 30px;
            height: 60px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #f0f2f5;
        }

        .layui-tab-title li {
            font-size: 16px;
            font-weight: 500;
            color: #666;
            height: 60px;
            line-height: 60px;
            padding: 0 20px;
            position: relative;
            cursor: pointer;
            transition: color 0.2s ease;
        }

        .layui-tab-title li.layui-this {
            color: #1E9FFF;
            font-weight: 600;
        }

        .layui-tab-title li.layui-this:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 20px;
            right: 20px;
            height: 3px;
            background-color: #1E9FFF;
            border-radius: 3px;
        }

        .module-header {
            padding: 25px 30px 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #f0f2f5;
        }

        .module-title {
            font-size: 20px;
            font-weight: 600;
            color: #333;
        }

        .module-actions {
            display: flex;
            gap: 10px;
        }

        .module-actions button {
            background-color: #1E9FFF;
            color: white;
            border: none;
            padding: 10px 18px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            font-size: 14px;
            font-weight: 500;
            box-shadow: 0 2px 8px rgba(30, 159, 255, 0.2);
        }

        .module-actions button:hover {
            background-color: #0d87e6;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(30, 159, 255, 0.3);
        }

        .module-actions button i {
            margin-right: 6px;
        }

        /* 卡片式布局样式 */
        .theme-cards {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 24px;
            padding: 30px;
        }

        .theme-card {
            background-color: #fff;
            border-radius: 14px;
            overflow: hidden;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        .theme-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
        }

        .card-image {
            width: 100%;
            height: 180px;
            object-fit: cover;
            transition: transform 0.5s ease;
        }

        .theme-card:hover .card-image {
            transform: scale(1.03);
        }

        .card-content {
            padding: 20px;
            flex-grow: 1;
            display: flex;
            flex-direction: column;
        }

        .card-status {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            margin-bottom: 12px;
            white-space: nowrap;
        }

        .status-running {
            background-color: rgba(30, 159, 255, 0.1);
            color: #1E9FFF;
        }

        .status-not-started {
            background-color: rgba(126, 136, 149, 0.1);
            color: #7E8895;
        }

        .status-finished {
            background-color: rgba(220, 53, 69, 0.1);
            color: #dc3545;
        }

        .status-deleted {
            background-color: rgba(220, 53, 69, 0.1);
            color: #676767;
        }

        .card-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 12px;
            color: #333;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .card-meta {
            display: flex;
            flex-direction: column;
            gap: 8px;
            margin-bottom: 20px;
            color: #666;
            font-size: 14px;
        }

        .meta-item {
            display: flex;
            align-items: center;
        }

        .meta-item i {
            color: #999;
            margin-right: 8px;
            width: 18px;
            text-align: center;
        }

        .card-actions {
            display: flex;
            gap: 10px;
            margin-top: auto;
        }

        .card-actions button {
            background-color: transparent;
            border: 1px solid #e0e2e5;
            cursor: pointer;
            padding: 8px 12px;
            border-radius: 6px;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            font-size: 13px;
            flex-grow: 1;
            justify-content: center;
        }

        .card-actions .edit-btn {
            color: #1E9FFF;
            border-color: #d0e8ff;
        }

        .card-actions .edit-btn:hover {
            background-color: #e6f2ff;
            border-color: #a8d5ff;
        }

        .card-actions .delete-btn {
            color: #dc3545;
            border-color: #f8d7da;
        }

        .card-actions .delete-btn:hover {
            background-color: #fce9eb;
            border-color: #f5c2c7;
        }

        /* 表格布局样式（用户管理模块） */
        .admin-table {
            width: 100%;
            border-collapse: collapse;
            margin: 30px;
        }

        .admin-table th, .admin-table td {
            padding: 18px 20px;
            text-align: left;
            border-bottom: 1px solid #f0f2f5;
        }

        .admin-table th {
            background-color: #f8f9fa;
            color: #666;
            font-weight: 500;
            font-size: 14px;
            position: relative;
        }

        .admin-table tr:last-child td {
            border-bottom: none;
        }

        .admin-table tr:hover {
            background-color: #fefefe;
        }

        .table-image {
            width: 50px;
            height: 50px;
            border-radius: 8px;
            object-fit: cover;
            border: 1px solid #e9ecef;
        }

        /* 分页样式 */
        .pagination-container {
            display: flex;
            justify-content: center;
            margin: 30px 0 20px;
        }

        .pagination {
            display: inline-flex;
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .pagination li {
            margin: 0 4px;
        }

        .pagination li a {
            display: inline-block;
            padding: 10px 16px;
            border-radius: 8px;
            text-decoration: none;
            color: #666;
            background-color: #f0f2f5;
            transition: all 0.2s ease;
            font-size: 14px;
            font-weight: 500;
        }

        .pagination li a:hover,
        .pagination li.active a {
            background-color: #1E9FFF;
            color: #fff;
            box-shadow: 0 2px 8px rgba(30, 159, 255, 0.2);
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .admin-container {
                padding: 20px 15px;
            }

            .layui-tab-title {
                padding: 0 15px;
            }

            .layui-tab-title li {
                padding: 0 15px;
                font-size: 15px;
            }

            .module-header {
                padding: 20px 15px 10px;
            }

            .module-title {
                font-size: 18px;
            }

            .theme-cards, .admin-table {
                padding: 15px;
            }

            .theme-card {
                border-radius: 12px;
            }

            .card-image {
                height: 150px;
            }

            .admin-table th, .admin-table td {
                padding: 15px 12px;
                font-size: 13px;
            }

            .pagination li a {
                padding: 8px 12px;
                font-size: 13px;
            }
        }

        @media (max-width: 480px) {
            .theme-cards {
                grid-template-columns: 1fr;
            }

            .module-actions {
                flex-direction: column;
                gap: 8px;
            }

            .module-actions button {
                justify-content: center;
            }

            .card-actions {
                flex-direction: column;
                gap: 8px;
            }
        }
    </style>
</head>
<body>
<nav class="header">

    <c:if test="${person.level == 1}">
        <a href="${pageContext.request.contextPath}/person/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">用户首页</span>
        </a>
        <a href="${pageContext.request.contextPath}/admin/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">管理员首页</span>
        </a>
    </c:if>
    <c:if test="${person.level == 0}">
        <a href="${pageContext.request.contextPath}/person/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">用户首页</span>
        </a>
    </c:if>

    <div class="ml-auto flex items-center">
        <c:if test="${person == null}">
            <a href="${pageContext.request.contextPath}/views/login.jsp"><i class="fa fa-user-circle mr-2"></i>请先登录</a>
        </c:if>
        <c:if test="${person != null}">
            <a href="${pageContext.request.contextPath}/admin/person/editPerson?pid=${person.pid}"><i
                    class="fa fa-user-circle mr-2"></i>${person.username}</a>
        </c:if>

        <a href="javascript:void(0);" onclick="logout()">退出登录</a>
    </div>
</nav>

<div class="admin-container">
    <!-- 使用Layui选项卡组件 -->
    <div class="layui-tab" lay-filter="adminTab">
        <ul class="layui-tab-title">
            <li class="layui-this">用户管理</li>
            <li>主题管理</li>
        </ul>
        <div class="layui-tab-content">
            <!-- 用户管理模块（表格布局） -->
            <div class="layui-tab-item layui-show">
                <div class="module-header">
                    <div class="module-title">用户管理</div>
                    <div class="module-actions">
                        <%--                        <button id="addUserBtn"><i class="fa fa-plus-circle"></i> 新增用户</button>--%>
                        <button id="searchUserBtn"><i class="fa fa-search"></i> 查询用户</button>
                    </div>
                </div>
                <table class="admin-table">
                    <thead>
                    <tr>
                        <th>用户ID</th>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        List<Person> personList = (List<Person>) session.getAttribute("personList");
                        if (personList != null) {
                            for (Person person : personList) {
                    %>
                    <tr>
                        <td><%= person.getPid() %>
                        </td>
                        <td><%= person.getUsername() %>
                        </td>
                        <td><%= person.getEmail() %>
                        </td>
                        <td><%= person.getDate() %>
                        </td>
                        <td>
                            <div class="action-buttons">
                                <button class="edit-btn" onclick="updatePerson( <%= person.getPid()   %>     )"><i
                                        class="fa fa-pencil"></i> 编辑
                                </button>
                                <button class="delete-btn" onclick="delPerson( <%= person.getPid()   %>     )"><i
                                        class="fa fa-trash"></i> 删除
                                </button>
                            </div>
                        </td>
                    </tr>
                    <%
                        }
                    } else {
                    %>
                    <tr>
                        <td colspan="3">暂无数据</td>
                    </tr>
                    <%
                        }
                    %>
                    </tbody>
                </table>
                <%--                <div class="pagination-container">--%>
                <%--                    <ul class="pagination">--%>
                <%--                        <li><a href="javascript:void(0);">上一页</a></li>--%>
                <%--                        <li class="active"><a href="javascript:void(0);">1</a></li>--%>
                <%--                        <li><a href="javascript:void(0);">2</a></li>--%>
                <%--                        <li><a href="javascript:void(0);">下一页</a></li>--%>
                <%--                    </ul>--%>
                <%--                </div>--%>
            </div>

            <!-- 主题管理模块（卡片式布局） -->
            <div class="layui-tab-item">
                <div class="module-header">
                    <div class="module-title">主题管理</div>
                    <div class="module-actions">
                        <button id="addTopicBtn"><i class="fa fa-plus-circle"></i> 新增主题</button>
                        <button id="searchTopicBtn"><i class="fa fa-search"></i> 查询主题</button>
                    </div>
                </div>

                <!-- 主题卡片区域 -->
                <div class="theme-cards">
                    <!-- 主题卡片：已结束 -->
                    <c:forEach var="topic" items="${sessionScope.topicList}">
                        <div class="theme-card" >
                            <img src="${pageContext.request.contextPath}${topic.url}" alt="${topic.title}"
                                 class="card-image" onclick="voteResult(${topic.tid})">
                            <div class="card-content">
                                    <%-- 0 未开始，1.正在进行 2. 已结束 3.删除（不显示）--%>
                                <c:if test="${topic.status == 0}">
                                    <span class="card-status status-not-started">未开始</span>
                                </c:if>
                                <c:if test="${topic.status == 1}">
                                    <span class="card-status status-running">进行中</span>
                                </c:if>
                                <c:if test="${topic.status == 2}">
                                    <span class="card-status status-finished">已结束</span>
                                </c:if>
                                <h3 class="card-title">${topic.title}</h3>
                                <div class="card-meta">
                                    <div class="meta-item"><i class="fa fa-user"></i> 创建者：${topic.author}</div>
                                    <div class="meta-item"><i class="fa fa-calendar"></i> ${topic.beginDate}
                                        至 ${topic.endDate}</div>
                                </div>
                                <div class="card-actions">
                                    <button onclick="editTopic(${topic.tid})" class="edit-btn"><i
                                            class="fa fa-pencil mr-2"></i> 编辑
                                    </button>
                                    <button onclick="delTop(${topic.tid})" class="delete-btn"><i
                                            class="fa fa-trash mr-2"></i> 删除
                                    </button>
                                </div>
                            </div>
                        </div>
                    </c:forEach>
                </div>
            </div>
        </div>
    </div>
    <p style="color: red">${delTopicMsg}</p>
    <p style="color: red">${delPersnMsg}</p>
</div>

<script>
    var form;
    var layer;
    var element;
    layui.use(['form', 'layer', 'element'], function () {
        form = layui.form;
        layer = layui.layer;
        element = layui.element;

    });

    // 模拟按钮点击事件
    document.querySelectorAll('.action-buttons button, .card-actions button').forEach(button => {
        button.addEventListener('click', function () {
            const action = this.textContent.trim();
            const module = element.tabGet('adminTab').title;
            layer.msg(`${module}：${action}功能已触发`, {icon: 1});
        });
    });

    //删除人员
    function delPerson(pid) {
        layer.confirm('确定要删除这个人员吗？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            // 这里添加退出登录的实际逻辑
            layer.msg('已成功删除', {icon: 1});
            setTimeout(function () {
                // window.location.href = '/VoingSystem/admin/topic/delTopic?tid=' + tid;
                window.location.href = '/VoingSystem/admin/person/delPerson?pid=' + pid;
            }, 1500);
            layer.close(index);
        })
    }

    //修改人员
    function updatePerson(pid) {
        window.location.href = '/VoingSystem/admin/person/editPerson?pid=' + pid;
    }


    // 删除主题
    function delTop(tid) {
        layer.confirm('确定要删除这个主题吗？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            // 这里添加退出登录的实际逻辑
            layer.msg('已成功删除', {icon: 1});
            setTimeout(function () {
                window.location.href = '/VoingSystem/admin/topic/delTopic?tid=' + tid;
                // window.location.href = '/VoingSystem/logout';
            }, 1500);
            layer.close(index);
        })
    }

    function editTopic(tid) {
        window.location.href = '/VoingSystem/admin/topic/editTopic?tid=' + tid;
    }


    document.getElementById('searchUserBtn').addEventListener('click', function () {
        layer.msg('查询用户功能已触发', {icon: 1});
        window.location.href = '/VoingSystem/views/admin/findPerson.jsp'
    });

    document.getElementById('addTopicBtn').addEventListener('click', function () {
        window.location.href = '/VoingSystem/views/topic/add.jsp';
        layer.msg('新增主题功能已触发', {icon: 1});
    });

    document.getElementById('searchTopicBtn').addEventListener('click', function () {
        layer.msg('查询主题功能已触发', {icon: 1});
        window.location.href = '/VoingSystem/views/topic/findTopic.jsp'
    });

    // var isFist = true
    // // 选项卡切换事件
    // element.on('tab(adminTab)', function (data) {
    //     if (isFist) {
    //         layer.msg(`切换到主题管理模块`, {icon: 1});
    //         isFist = !isFist
    //     } else {
    //         layer.msg(`切换到用户模块`, {icon: 1});
    //         isFist = !isFist
    //     }
    // });
    function logout() {
        layer.confirm('确定要退出登录吗？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            // 这里添加退出登录的实际逻辑
            layer.msg('已成功退出登录', {icon: 1});
            setTimeout(function () {
                window.location.href = '/VoingSystem/logout';
            }, 1500);
            layer.close(index);
        })
    }

    function voteResult(tid) {
        window.location.href = '${pageContext.request.contextPath}/vote/show?tid=' + tid
    }
</script>
</body>
</html>